package com.xindaoapp.happypet.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

public class CommonViewPagerAdapter extends FragmentPagerAdapter implements OnPageChangeListener {
    Fragment[] fragments;
    View[] tabViews;

    public CommonViewPagerAdapter(FragmentManager fm, Fragment[] fragments, View[] tabViews) {
        super(fm);
        this.fragments = fragments;
        this.tabViews = tabViews;
        setTabSelected(0);
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragments[arg0];
    }

    @Override
    public int getCount() {
        return fragments.length;
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {}

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {}

    @Override
    public void onPageSelected(int position) {
        setTabSelected(position);
    }

    private void setTabSelected(int position) {
        for (int i = 0; i < tabViews.length; i++) {
            if (i == position) {
                tabViews[i].setSelected(true);
            } else {
                tabViews[i].setSelected(false);
            }
        }
    }
}
